define([
    "dojo/_base/declare",
    "dijit/_Widget",
    "dijit/_Templated",
    "dojo/text!./ProductGrid/template/grid.html",
    "dojo/_base/kernel",
    "dojox/grid/DataGrid",
    "dojo/data/ItemFileWriteStore",
    "dojo/behavior",
    "dijit/form/TextBox",
    "dijit/form/Select",
    "dijit/form/Button",
    
    "dijit/form/SimpleTextarea"],
    function(declare, _Widget,_Templated,templateHtml,dojo,DataGrid,ItemFileWriteStore,Behavior){
        return declare("widget.ProductGrid",[_Widget,_Templated], {
            widgetsInTemplate: true,
            baseClass: "product-grid",
            templateString: templateHtml,
            grid:null,
            initGrid:function(){
                var layout = [[
                {
                    'name': 'Ref#', 
                    'field': 'Ref',
                    'width': "10%",
                    formatter: function(text,row,object){
                        var id = object.grid.getItem(row).ID[0];
                        return "<a class='view' product='"+id+"' href='javascript:void(0)'>"+text+"</a>";
                    }
                },

                {
                    'name': 'Photo', 
                    'field': 'Photo' ,
                    'width': "10%",
                    formatter: function(text){
                        if (text){
                            return "<img width='50px' height='50px' src='"+djConfig.image + text +"' />";
                        }else{
                            return "";
                        }
                    }
                },

                {
                    'name': 'Type', 
                    'width': "10%",
                    'field': 'Type'
                },

                {
                    'name': 'Usage', 
                    'width': "10%",
                    'field': 'Usage'
                },

                {
                    'name': 'Style', 
                    'width': "10%",
                    'field': 'Style'
                },

                {
                    'name': 'Price', 
                    'width': "10%",
                    'field': 'Price'
                },

                {
                    'name': 'Price Level', 
                    'width': "10%",
                    'field': 'PriceLevel'
                },
                {
                    'name': 'Unit Weight', 
                    'width': "10%",
                    'field': 'UnitWeight'
                },

                {
                    'name': ' ',
                    'width': "10%",
                    'field': 'ID',
                    formatter: function(text){
                        return "<a class='edit' product='"+text+"' href='javascript:void(0)'>Edit</a>&nbsp;&nbsp;<a product='"+text+"' class='delete' href='javascript:void(0)'>Remove</a>";
                    }
                }
                ]];
                this.grid = new DataGrid({
                    id: 'product_grid',
                    structure: layout,
                    selectionMode:"none",
                    autoHeight:true,
                    onHeaderClick : function(){
                        Behavior.apply();
                    }
                },
                document.createElement('div'));
                dojo.byId("product_grid_div").appendChild(this.grid.domNode);
                this.grid.startup();
            },
            
            loadGrid: function(){
                var data = {};
                var xhrArgs = {
                    url:"product/getproductlist/",
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    data.identifier = 'ID';
                    data.items = result;
                    var store = new ItemFileWriteStore({data: data});
                    this.grid.setStore(store);
                    Behavior.apply();
                }));
            },
            
            bindAction:function(){
                var that = this;
                Behavior.add({
                    ".product-grid a.view" : {
                        onclick: function(){
                            var id = dojo.getAttr(this,"product");
                            that.viewProduct(id);
                        }
                    },
                    ".product-grid a.edit" : {
                        onclick: function(){
                            var id = dojo.getAttr(this,"product");
                            that.editProduct(id);
                        }
                    },
                    ".product-grid a.delete" : {
                        onclick: function(){
                            var id = dojo.getAttr(this,"product");
                            that.deleteProduct(id);
                        }
                    }
                });
                
                dojo.connect(this.newButton,"onClick",dojo.hitch(this,this.newProduct));
                
                dojo.connect(this.exportButton,"onClick",dojo.hitch(this,function(){
                    location.href = "product/export";
                }));
                
                dojo.subscribe("ProductList/Show", null,dojo.hitch(this,function(object){
                    this.loadGrid();
                    dojo.style(this.domNode, "display", "block");
                }));
            },
            
            newProduct:function(){
                dojo.style(this.domNode, "display", "none");
                dojo.publish("ProductForm/Show",{mode:'new'});
            },
            
            viewProduct: function(id){
                dojo.style(this.domNode, "display", "none");
                dojo.publish("ProductForm/Show", {mode:'view',id:id});
            },
            
            editProduct: function(id){
                dojo.style(this.domNode, "display", "none");
                dojo.publish("ProductForm/Show", {mode:'edit',id:id});
            },
            
            deleteProduct: function(id){
                var xhrArgs = {
                    url:"product/deleteproduct/pid/"+id,
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    this.loadGrid();
                }));
            },
            
            postCreate: function(){
                this.inherited(arguments);
                this.initGrid();
                this.loadGrid();
                this.bindAction();
            }
        });
    });